/*
 * Copyright (C) Michael Martin & Antonio Carrozzini (TBJD)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.tbjd.client.guis.gantt.items;

import java.util.HashMap;
import java.util.Map;

import org.tbjd.client.beans.Resource;
import org.tbjd.client.constants.GanttConstants;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.exceptions.NoUpperDecisionmakerFoundException;
import org.tbjd.client.exceptions.NoUpperMediatorFoundException;
import org.tbjd.client.interfaces.Builder;
import org.tbjd.client.interfaces.Decisionmaker;
import org.tbjd.client.listeners.ganttResourceItem.GanttResourceItemRatioChangeListener;

import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
/**
 * <p>
 * A {@link org.tbjd.client.beans.Resource Resource} item associated to a {@link org.tbjd.client.beans.Task Task}
 * </p>
 * <p>
 * A panel containing the initials and ratio of the Resource displayed to the right of a {@link org.tbjd.client.guis.gantt.items.GanttTaskItem GanttTaskItem}
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */

//TODO must implements Item interface
public class GanttResourceItem extends FocusPanel implements Builder, Decisionmaker {

	/**
	 * The id of the {@link org.tbjd.client.beans.Task Task} it is associated with
	 */
	private long taskId;
	/**
	 * The {@link org.tbjd.client.beans.Resource Resource} bean
	 */
	private Resource resource;

	private HorizontalPanel container = new HorizontalPanel();
	private FocusPanel initialsPanel = new FocusPanel();
	private HorizontalPanel ratioPanel = new HorizontalPanel();

	public GanttResourceItem() {
		build();
	}

	public void build() {
		// set styles
		this.container.setStyleName("ganttResourceItem");
		this.initialsPanel.setStyleName("ganttResourceItem_initialsPanel");
		this.ratioPanel.setStyleName("ganttResourceItem_ratioPanel");

		this.container.setHeight(GanttConstants.DRAGGABLEGANTTITEM_HEIGHT-4 + "px");
		this.initialsPanel.setHeight(GanttConstants.DRAGGABLEGANTTITEM_HEIGHT-4 + "px");
		this.ratioPanel.setHeight(GanttConstants.DRAGGABLEGANTTITEM_HEIGHT-4 + "px");

		this.container.setCellHorizontalAlignment(this.initialsPanel, HasHorizontalAlignment.ALIGN_CENTER);
		this.container.setCellHorizontalAlignment(this.ratioPanel, HasHorizontalAlignment.ALIGN_CENTER);

		// build
		this.container.add(this.initialsPanel);
		this.container.add(this.ratioPanel);

		this.add(this.container);
	}

	public long getTaskId() {
		return taskId;
	}

	public void setTaskId(long taskId) {
		this.taskId = taskId;
	}

	/**
	 * Sets the {@link org.tbjd.client.beans.Resource Resource} infos (initials and ratio)
	 */
	public void setResource(Resource resource) {
		this.resource = resource;

		Label lblName = new Label(this.resource.getInitials());
		this.initialsPanel.add(lblName);
		
		TextBox txtRatio = new TextBox();
		txtRatio.setHeight(GanttConstants.DRAGGABLEGANTTITEM_HEIGHT-4 + "px");
		txtRatio.setStyleName("ganttResourceItem_ratioPanel_txtBox");
		txtRatio.setText(this.resource.getRatio() + "");
		txtRatio.addChangeListener(new GanttResourceItemRatioChangeListener());
		this.ratioPanel.add(txtRatio);
		
		Label txtPercent = new Label("%");
		txtPercent.setHeight(GanttConstants.DRAGGABLEGANTTITEM_HEIGHT-4 + "px");
		txtPercent.setStyleName("ganttResourceItem_ratioPanel_percent");
		this.ratioPanel.add(txtPercent);
		this.ratioPanel.setCellVerticalAlignment(txtRatio, HasVerticalAlignment.ALIGN_MIDDLE);
		this.ratioPanel.setCellVerticalAlignment(txtPercent, HasVerticalAlignment.ALIGN_MIDDLE);
	}

	public Integer getResourceId() {
		return this.resource.getId();
	}

	public FocusPanel getHandle() {
		return this.initialsPanel;
	}

	public void decide(Object action) {
		System.out.println("GanttResourceItem.decide()");

		if (((Map<?, ?>) action).containsKey(NotificationsConstants.TASK_RESOURCES_DRAGGING)) {
			// change resource's ratio
			this.resource.setRatio((Integer) ((Map<?, ?>) action).get(NotificationsConstants.TASK_RESOURCES_DRAGGING));

			// create a map with the updated resource
			Map<Integer, Object> map = new HashMap<Integer, Object>();
			map.put(NotificationsConstants.TASK_RESOURCES_DRAGGING, this);

			try {
				delegate(map);
			} catch (NoUpperDecisionmakerFoundException e) {
				e.printStackTrace();
			} catch (NoUpperMediatorFoundException e) {
				e.printStackTrace();
			}
		}
	}

	public void delegate(Object action) throws NoUpperDecisionmakerFoundException, NoUpperMediatorFoundException {
		System.out.println("GanttResourceItem.delegate()");

		// passes info to the first responsible
		Widget parent = ((Widget) this).getParent();
		while (parent != null && parent instanceof Decisionmaker == false) {
			parent = ((Widget) parent).getParent();
		}
		if (parent != null) {
			try {
				((Decisionmaker) parent).decide(action);
			} catch (TBJDException e) {
				e.printStackTrace();
			}
		} else {
			throw new NoUpperDecisionmakerFoundException();
		}
	}

	public void handle(Object action) {
	}

	public Widget getBuildedWidget() {
		return this.container;
	}

	public Resource getResource() {
		return resource;
	}

}
